"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskTasksettingCtrl
 * @description
 * # TaskTasksettingCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "TaskTasksettingCtrl",
    function(localStorageService, openApi, DataService, $location, Util) {
      var vm = this;
      vm.editcontent = function() {
        localStorageService.set("taskdetail", vm.data);
        $location.path("task/edittask");
      };
      this.choosepeople = function() {
        setTaskData();
        // getFollows是要访问的接口
        $location.path("chooseuser/index/getFollows");
      };
      var setTaskData = function() {
        var userskey = {
          name: "personName",
          id: "userId"
        };
        localStorageService.set("userskey", userskey);
        localStorageService.set("chooseUsrs", vm.data.participantList);
        localStorageService.set("newtask", vm.data);
      };

      var init = function() {
        vm.data = localStorageService.get("taskdetail");
        var chooseUsrs = localStorageService.get("chooseUsrs");
        localStorageService.remove("chooseUsrs");
        vm.data.participantList = chooseUsrs || vm.data.participantList;
        var selectData;
        selectData = Util.getDataforsearch();
        vm.data = angular.extend(vm.data, selectData.filterData);
        vm.showrelModule();
      };
      vm.save = function() {
        var param = {};
        param.pkId = vm.data.pkId;
        param.leaderId = vm.data.leaderId;
        param.content = vm.data.content;
        param.expiryDate = vm.data.expiryDate;
        param.attachment = vm.data.attachment;
        param.relatedModuleName = vm.data.relatedModuleName;
        param.relatedModule = vm.data.relatedModule;
        param.moduleNo = vm.data.moduleNo;
        if(!param.content){
          Util.Toast("任务详情不能为空");
          return
        }
        if(!param.leaderId){
          Util.Toast("负责人不能为空");
          return
        }
        if(!param.expiryDate){
          Util.Toast("截至日期不能为空");
          return
        }
        param.participantIds = vm.data.participantList.map(function(data) {
          return data.userId;
        });
        // 不显示loader
        param.noloader = true;
        DataService.getData(openApi.updateTask, param).then(function(data) {
          Util.Toast(data);
        });
      };
      this.confirmdel = function() {
        if (navigator.notification) {
          navigator.notification.confirm("是否删除", vm.deltask, "提示", [
            "删除",
            "取消"
          ]);
        }
      };
      vm.delpersons = function(index) {
        vm.data.participantList.splice(index, 1);
      };
      vm.deltask = function(btn) {
        if (btn == 1) {
          var param = {};
          param.pkId = vm.data.pkId;
          DataService.getData(openApi.delTask, param).then(function(data) {
            Util.Toast(data, { callback: Util.goBack });
          });
        }
      };
      vm.chooseinfo = function() {
        // $routeParams.id通过id确定唯一回显id
        if (!vm.data.relatedModule || vm.data.relatedModule == "无") {
          Util.Toast("请选择关联模块");
          return;
        }
        // 里面包含了title
        var relModuleObj = {
          selectTab: vm.data.relatedModule,
          selectTabContent: vm.data.relatedModuleName
        };
        Util.savaDataforsearch(relModuleObj);
        setTaskData();
        $location.path("finance/choosepeople"+"/"+vm.functionId);
      };
      vm.showrelModule = function() {
        Util.getModuleRel(Util.RW_MODULE, function(data) {
          vm.relmodlist = data.data;
          if(vm.data.relatedModuleName){
            vm.functionId =vm.relmodlist.filter(function(item){
              return item.key==vm.data.relatedModuleName
            })[0]["flag"]    
          }
        });
      };
      vm.modulechange = function() {
        vm.data.relatedModule = Util.getValueFromkey(
          vm.relmodlist,
          vm.data.relatedModuleName
        );
        vm.functionId =vm.relmodlist.filter(function(item){
          return item.key==vm.data.relatedModuleName
        })[0]["flag"]
        vm.data.moduleNo = "";
      };
      init();
    }
  );
